//
//  DatabaseTool.m
//  OrderDish
//
//  Created by mac on 16/11/25.
//  Copyright © 2016年 LiWenqing. All rights reserved.
//

#import "DatabaseTool.h"
#import "FMDatabase.h"

FMDatabase *_db;
@implementation DatabaseTool
+(void)openDatabase{
    NSString *sourcePath=[[NSBundle mainBundle]pathForResource:@"database" ofType:@"sqlite"];
    
    NSString *destinationPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"database.sqlite"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
        //在目的路径不存在的存在下,复制一份到目的路径
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:nil];
    }
    _db=[[FMDatabase alloc]initWithPath:destinationPath];
    if ([_db open]) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
 

}
+(void)deleteFile{
    //获取要移除的文件夹路径
    NSString *myFile =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"database.sqlite"];
    
    //创建文件管理器
    NSFileManager *manager =[NSFileManager defaultManager];
    
    //判断要移除的文件夹是否存在
    BOOL isExist = [manager fileExistsAtPath:myFile];
    if(isExist == YES)
    {
        //如果文件存在直接移除
        NSError *error = nil;
        //参一：传入要移除的文件夹
        BOOL removeResult = [manager removeItemAtPath:myFile error:&error];
        
        //判断是否移除成功
        if(removeResult == YES)
        {
            NSLog(@"移除成功");
        }
        else
        {
            NSLog(@"移除失败，原因%@",error);
        }
    }
    else
    {
        NSLog(@"不存在文件");
    }
}
+(NSArray *)selectGroupFromGroupTable{

    NSString *sql=@"select * from groupTable";
    FMResultSet *set=[_db executeQuery:sql];
    NSMutableArray *array=[NSMutableArray array];
    
    while ([set next]) {
        GroupModel *group=[[GroupModel alloc]init];
        group.GroupID=[set intForColumnIndex:0];
        group.kind=[set stringForColumnIndex:1];
        group.name=[set stringForColumnIndex:2];
        group.img=[set stringForColumnIndex:3];
        group.hImg=[set stringForColumnIndex:4];
        
        [array addObject:group];
    }
    [set close];
    return array;
}
+(NSArray *)selectFromGroupTableWithGroupID:(NSInteger)groupID{
    NSString *sql=[NSString stringWithFormat:@"select *from grouptable where id=%d",groupID];
    FMResultSet *set=[_db executeQuery:sql];
    [set next];
    NSString *name=[set stringForColumn:@"name"];
    [set close];
    
    return [name componentsSeparatedByString:@"|"];
}
+(NSArray *)selectfromMenuTableWithIKind:(NSString *)ikind{
    NSString *sql=[NSString stringWithFormat:@"select *from menutable where iKind='%@'",ikind];
    FMResultSet *set=[_db executeQuery:sql];
    NSMutableArray *array=[NSMutableArray array];
    
    while ([set next]) {
        DishModel *dish=[[DishModel alloc]init];
        dish.dishID=[set intForColumnIndex:0];
        dish.groupID=[set intForColumnIndex:1];
        dish.iKind=[set stringForColumnIndex:2];
        dish.name=[set stringForColumnIndex:3];
        dish.price=[set stringForColumnIndex:4];
        dish.unit=[set stringForColumnIndex:5];
        dish.detail=[set stringForColumnIndex:6];
        dish.picName=[set stringForColumnIndex:7];
        
        [array addObject:dish];
    }
    [set close];
    return array;
}
@end
